;
(function(){
	
	var OutBtns,
		proLis,
		liBtns,
		compLis,
		compLay = '<div class="comp_lay J_comOutLay"><div class="comp_con J_compLay">'+
			'<a href="#" class="li_comp_btn btn-gre-big disable J_finaBtn">Compare</a>'+
			'<a href="#" class="close J_comClose">close</a>'+
			'</div></div>',
		comEle,
		addEle,
		proId,
		proImg,
		proTi,
		proLink,
		proPrice,
		addNum,
		addedLi,
		errTip = '<span class="error-tip J_errorTip">please select items!</span>',
		idList = '';
		
	var doCompare = function () {
		bindOutBtn();
		addComp();		
	};
	
	var bindOutBtn = function () {
		OutBtns = $('.J_compBtn');	
		
		$(OutBtns).each(function(i){
			$(OutBtns[i]).bind('click', function(e){
				var me = $(this);
				
				addedLi = $('.J_added');
				
				if(addedLi.length === 0){
					
					e.preventDefault();
					$(errTip).insertAfter($(this));	
					
					setTimeout(function(){
						
						$(".J_errorTip").fadeOut("slow",function(){
						  $(this).remove();	 
						});
					},2000);
				}else{
					toComp(me);		
				}
			});				
		});
	};
	
	var addComp = function () {
		liBtns = $('.J_liBtn');
		
		$(liBtns).each(function(i){
			$(liBtns[i]).change(function(e){
				
				comEle = $('.J_compLay'),
				proLis = $(this).parents('.J_proLi'),
				proId = $(proLis).attr('data-id'),
				addNum = $('.J_added').length;
				
				if(comEle.length === 0){
					$('body').append(compLay);	
					bindFinaBtn();
					bindClose();	
				}
									
				if($(this).attr('checked') ===  'checked'){
					
					if(addNum < 4){
						addPro();		
					}else{
						$(this).removeAttr('checked');
					}
				}else if(!$(this).attr('checked')){
					removePro();	
				}
				
				chaBtnSta();
				
				return false;
			});	
		});		
	};
	
	var bindFinaBtn = function () {
		
		$('.J_finaBtn').bind('click', function(e){
			var me = $(this);
			if(addedLi.length > 0){
				toComp(me);	
			}else{
				e.preventDefault();	
			}	
		});		
	};
	
	var addPro = function () {
		proImg = $(proLis).find('.J_proImg').attr('src'),
		proTi = $(proLis).find('.J_proTitle').text(),
		proLink = $(proLis).find('.J_proTitle').attr('href'),
		proPrice = $(proLis).find('.cu-price').text();	
		
		addEle = '<div class="add-li J_added" data-id="' + proId + '">' +
			'<div class="li-img"><a target="_blank" href="' +
			 proLink + '"><img src="' +
			 proImg + '" /></a></div>' +
			 '<p class="li-info"><a target="_blank" href="' +
			  proLink + '">' + proTi +
			  '</a><span>' + proPrice + '</span></p>' +
			'</div>';
		
		$('.J_compLay').append(addEle);
	};
	
	var removePro = function () {
		addedLi = $('.J_added');
		
		$(addedLi).each(function(i){
			
			if($(addedLi[i]).attr('data-id') === proId){
				
				$(addedLi[i]).remove();	
			}	
		});	
	};
	
	var chaBtnSta = function () {
		addedLi = $('.J_added');
		
		if(addedLi.length === 0){
			$('.J_finaBtn').addClass('disable');	
		}else{
			$('.J_finaBtn').removeClass('disable');	
		}		
	};
	
	var toComp = function (me) {
		idList = '';
		for(i=0,u=addedLi.length; i<u; i++){
			idList += $(addedLi[i]).attr('data-id') + ',' ;	
		}
		
		$(me).attr({
			'target' : '_blank',
			'href' :  _lig_config.compareUrl + '?proId=' + idList	
		});
	};
	
	var bindClose = function () {
		$('.J_comClose').bind('click', function(e){
			e.preventDefault();
			
			$('.J_comOutLay').remove();	
			
			$(liBtns).each(function(i){
				$(liBtns[i]).removeAttr('checked');
			});	
		});	
	};
	
	$(document).ready(function(){
		new doCompare();	
	});	
})();